package com.example.translateappjava.Activity.KnowledgeFragmentActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.translateappjava.Adapter.NewsInfo;
import com.example.translateappjava.DataBase.CollectDbHelper;
import com.example.translateappjava.Fragment.MineFragment;
import com.example.translateappjava.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;

public class NewsDetailsActivity extends AppCompatActivity {
    private NewsInfo.ResultBean.DataBean dataBean;
    private Toolbar toolbar;
    private WebView mWebView;
    private FloatingActionButton collection;

    private FloatingActionButton floatingActionButton;

    private int newsViewCount = 0; // 新闻浏览次数计数器

    public int getNewsViewCount() {
        return newsViewCount;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_news_details);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        toolbar = findViewById(R.id.toolbar);
        mWebView = findViewById(R.id.webView);
        collection = findViewById(R.id.collection);
        floatingActionButton = findViewById(R.id.floatingActionButton2);
        collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null!=dataBean){

                    //collect
                    String dataBeanJson = new Gson().toJson(dataBean);
                    CollectDbHelper.getInstance(NewsDetailsActivity.this).addCollect(null,dataBean.getUniquekey(),dataBeanJson);
                    Toast.makeText(NewsDetailsActivity.this,"收藏成功，前往\"我的收藏\"查看",Toast.LENGTH_SHORT).show();

                }
            }
        });

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(NewsDetailsActivity.this,"点赞成功",Toast.LENGTH_SHORT).show();
            }
        });

        dataBean = (NewsInfo.ResultBean.DataBean) getIntent().getSerializableExtra("dataBean");

        if(null!=dataBean){
            toolbar.setTitle(dataBean.getTitle());
            mWebView.loadUrl(dataBean.getUrl());
            newsViewCount++; // 当加载新闻时，增加一个计数器

        }

        //fanhui
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



    }
}